package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.ProductMaterial;
import com.uwlaser.service.ProductMaterialService;
import com.uwlaser.service.dto.ProductMaterialDto;
import com.uwlaser.service.dto.ProductMaterialQueryParam;
import com.uwlaser.service.mapper.ProductMaterialMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = ProductMaterialService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProductMaterialServiceImpl extends CommonServiceImpl<ProductMaterialMapper, ProductMaterial> implements ProductMaterialService {

    // private final RedisUtils redisUtils;
    private final ProductMaterialMapper productMaterialMapper;

    @Override
    public PageInfo<ProductMaterialDto> queryAll(ProductMaterialQueryParam query, Pageable pageable) {
        IPage<ProductMaterial> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<ProductMaterial> page = productMaterialMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, ProductMaterialDto.class);
    }

    @Override
    public List<ProductMaterialDto> queryAll(ProductMaterialQueryParam query){
        return ConvertUtil.convertList(productMaterialMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ProductMaterialDto.class);
    }

    @Override
    public List<ProductMaterialDto> queryNotBindList(ProductMaterialDto param) {
        return productMaterialMapper.selectNotBindList(param);
    }

    @Override
    public ProductMaterial getById(Integer id) {
        return productMaterialMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public ProductMaterialDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), ProductMaterialDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProductMaterialDto resources) {
        ProductMaterial entity = ConvertUtil.convert(resources, ProductMaterial.class);
        ProductMaterial productMaterial = new ProductMaterial();
        productMaterial.setMaterialCode(entity.getMaterialCode());
        Integer integer = productMaterialMapper.selectCount(new QueryWrapper<>(productMaterial));
        if(integer > 0){
            throw new RuntimeException("物料编码【"+productMaterial.getMaterialCode()+"】已存在！");
        }
        return productMaterialMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(ProductMaterialDto resources){
        ProductMaterial entity = ConvertUtil.convert(resources, ProductMaterial.class);
        int ret = productMaterialMapper.updateById(entity);
        ProductMaterial productMaterial = new ProductMaterial();
        productMaterial.setMaterialCode(entity.getMaterialCode());
        Integer integer = productMaterialMapper.selectCount(new QueryWrapper<>(productMaterial));
        if(integer >= 2){
            throw new RuntimeException("物料编码【"+productMaterial.getMaterialCode()+"】已存在！");
        }
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return productMaterialMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<ProductMaterialDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ProductMaterialDto productMaterial : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("物料编码", productMaterial.getMaterialCode());
              map.put("物料简码", productMaterial.getMaterialBriefCode());
              map.put("物料名称", productMaterial.getMaterialName());
              map.put("物料类型", productMaterial.getMaterialType());
              map.put("物料规格", productMaterial.getMaterialSpecification());
              map.put("物料来源", productMaterial.getMaterialSource());
              map.put("品牌", productMaterial.getMaterialBrand());
              map.put("单位", productMaterial.getMaterialUnit());
              map.put("供应商编号", productMaterial.getSupplier());
              map.put("供应商名称", productMaterial.getSupplierName());
              map.put("描述", productMaterial.getRemark());
              map.put("创建人", productMaterial.getCreateBy());
              map.put("创建时间", productMaterial.getCreateTime());
              map.put("更新人", productMaterial.getUpdateBy());
              map.put("更新时间", productMaterial.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
